<template>
    <div class="gou">
      <el-table
        :data="pageInfo.list"
        style="width: 100%">
        <el-table-column
          prop="gname"
          label="商品名称"
          >
        </el-table-column>
        <el-table-column
          prop="price"
          label="价格"
         >
        </el-table-column>
        <el-table-column
          prop="num"
          label="数量">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.row)">下单</el-button>

          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageInfo.pageNum"
        :page-sizes="[2, 200, 300, 400]"
        :page-size="pageInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="pageInfo.total">
      </el-pagination>

      <el-dialog title="选购数量" :visible.sync="dialogFormVisible">
        <el-form :model="forms">
          <el-form-item label="数量" :label-width="formLabelWidth">
            <el-input v-model="forms.shu" type="number" autocomplete="off" style="width: 200px"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="dialogFor" :disabled="forms.shu<0">确定</el-button>
        </div>
      </el-dialog>
    </div>
</template>

<script>
    export default {
        name: "Gou",
      data(){
          return{
            dialogFormVisible:false,
            formLabelWidth:"200px",
            forms:{
              did:"",
              shu:"",
              gid:"",
            },
            pageInfo:{},
            from:{
              pageNum:1,
              pageSize:2,
            },
            id:"",
            price:"",
            bmoney:"",
            num:"",
          }
      },
      methods:{
        dialogFor(){
          this.forms.did=this.$route.query.id;
          console.log( this.forms.did);
          if (this.$route.query.bmoney>=this.forms.shu*this.price) {
if (this.num>=this.forms.shu){
            this.axios.post(`http://localhost:9001/api/good/update`,this.forms).then(res=>{
              if (res.data.code==200){
                this.initData();
                this.dialogFormVisible=false;
                this.$message.success("下单成功");
              } else {
                this.$message.error("下单失败");
              }
            })
}else {
  this.$message.error("商品数量不足，超出"+(this.forms.shu-this.num)+"个");
}
          }else {
            this.$message.error("钱不够，请尽快充值")
          }


        },
        handleEdit(row){
          this.dialogFormVisible=true;
          this.forms={};
          this.forms.gid=row.gid;
          this.price=row.price;
          this.num=row.num;
      },
        handleSizeChange(val){
          this.from.pageSize=val;
          this.initData();
        },
        handleCurrentChange(val){
          this.from.pageNum=val;
          this.initData();
        },
initData(){
  this.axios.post(`http://localhost:9001/api/good/findAll?pageNum=${this.from.pageNum}&pageSize=${this.from.pageSize}`).then(res=>{
    this.pageInfo=res.data;
  })
}
      },
      created(){
          this.id=this.$route.query.id;
          this.bmoney=this.$route.query.bmoney;
this.initData();
      }
    }
</script>

<style scoped>

</style>
